package com.marvels.financial.consum.impl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONObject;
import com.marvels.approve.loan.api.LoanApplyProvideApi;
import com.marvels.approve.loan.dto.CascadeChannelDto;
import com.marvels.approve.loan.service.IMcamLoanApplyInfoService;
import com.marvels.approve.loan.service.LoanApplicationService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.consum.FinancialConsumerApproveApi;
import com.marvels.financial.loan.dto.InsurancePolicyDTO;
import com.marvels.financial.loan.dto.McamLoanApplyContactInfo;
import com.marvels.financial.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.financial.loan.dto.ProtocolTemplateInfoDTO;
import com.marvels.financial.loan.dto.WaitLendInfoDTO;

import cn.hutool.core.bean.BeanUtil;



@Component
public class FinancialConsumerApproveImpl implements FinancialConsumerApproveApi{

	@Autowired
	private LoanApplyProvideApi loanApplyProvideApi;
	@Autowired
	private LoanApplicationService  loanApplicationService;
	
	@Override
	public int modifyLoanApplyInfo(McamLoanApplyInfoDTO mcamLoanApplyInfoDTO) {
		try {
			com.marvels.approve.loan.dto.McamLoanApplyInfoDTO param = new com.marvels.approve.loan.dto.McamLoanApplyInfoDTO();
			BeanUtil.copyProperties(mcamLoanApplyInfoDTO, param);
			int result = loanApplyProvideApi.modifyLoanApplyInfo(param);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.modifyLoanApplyInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.modifyLoanApplyInfo error", e);
			return 0;
		}
	}

	@Override
	public McamLoanApplyInfoDTO selectLoanApplyInfo(McamLoanApplyInfoDTO mcamLoanApplyInfoDTO) {
		try {
			com.marvels.approve.loan.dto.McamLoanApplyInfoDTO param = new com.marvels.approve.loan.dto.McamLoanApplyInfoDTO();
			BeanUtil.copyProperties(mcamLoanApplyInfoDTO, param);
			com.marvels.approve.loan.dto.McamLoanApplyInfoDTO apiResult = loanApplyProvideApi.selectLoanApplyInfo(param);
			McamLoanApplyInfoDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McamLoanApplyInfoDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.selectLoanApplyInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.selectLoanApplyInfo error", e);
			return null;
		}
	}

	@Override
	public List<McamLoanApplyContactInfo> selectContactByLoanId(McamLoanApplyContactInfo mcamLoanApplyContactInfo) {
		try {
			com.marvels.approve.loan.dto.McamLoanApplyContactInfo param = new com.marvels.approve.loan.dto.McamLoanApplyContactInfo();
			BeanUtil.copyProperties(mcamLoanApplyContactInfo, param);
			List<com.marvels.approve.loan.dto.McamLoanApplyContactInfo> apiResult = loanApplyProvideApi.selectContactByLoanId(param);
			List<McamLoanApplyContactInfo> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McamLoanApplyContactInfo.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.selectContactByLoanId direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.selectContactByLoanId error", e);
			return null;
		}
	}

	@Override
	public List<InsurancePolicyDTO> selectInsuranceByLoanId(
			InsurancePolicyDTO mcamLoanInsurancePolicyInfo) {
		try {
			com.marvels.approve.common.dto.InsurancePolicyDTO param = new com.marvels.approve.common.dto.InsurancePolicyDTO();
			BeanUtil.copyProperties(mcamLoanInsurancePolicyInfo, param);
			List<com.marvels.approve.common.dto.InsurancePolicyDTO> apiResult = loanApplyProvideApi.selectInsuranceByLoanId(param);
			List<InsurancePolicyDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), InsurancePolicyDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.selectInsuranceByLoanId direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.selectInsuranceByLoanId error", e);
			return null;
		}
	}


	@Override
	public MarvelsResultDTO loanOperation(String loanIds) {
		try {
			MarvelsResultDTO result = loanApplicationService.executeLoanApply(loanIds);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.loanOperation direct call success!");
			return result;
		} catch(Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.loanOperation error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "放款处理异常", null);
		}
	}

	@Override
	public List<String> selectWaitLendInfo(WaitLendInfoDTO infoDTO) {
		try {
			com.marvels.approve.loan.dto.WaitLendInfoDTO param = new com.marvels.approve.loan.dto.WaitLendInfoDTO();
			BeanUtil.copyProperties(infoDTO, param);
			List<String> result = loanApplyProvideApi.selectWaitLendInfo(param);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.selectWaitLendInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.selectWaitLendInfo error", e);
			return null;
		}
	}

	@Override
	public List<ProtocolTemplateInfoDTO> findProtocalTemplateList(McamLoanApplyInfoDTO infoDTO) {
		try {
			com.marvels.approve.loan.dto.McamLoanApplyInfoDTO param = new com.marvels.approve.loan.dto.McamLoanApplyInfoDTO();
			BeanUtil.copyProperties(infoDTO, param);
			List<com.marvels.approve.process.dto.ProtocolTemplateInfoDTO> apiResult = loanApplyProvideApi.findProtocalTemplateList(param);
			List<ProtocolTemplateInfoDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), ProtocolTemplateInfoDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.findProtocalTemplateList direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.findProtocalTemplateList error", e);
			return null;
		}
	}

	
	@Override
	public Map<String, Object> selectShzxNeedLoanApplyInfo(McamLoanApplyInfoDTO mcamLoanApplyInfoDTO) {
		try {
			com.marvels.approve.loan.dto.McamLoanApplyInfoDTO param = new com.marvels.approve.loan.dto.McamLoanApplyInfoDTO();
			BeanUtil.copyProperties(mcamLoanApplyInfoDTO, param);
			Map<String, Object> result = loanApplyProvideApi.selectShzxNeedLoanApplyInfo(param);
			MarvelsLogUtil.getInstance().info("FinancialConsumerApproveApi.selectShzxNeedLoanApplyInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerApproveApi.findProtocalTemplateList error", e);
			return null;
		}
	}
}

